package com.viethp.mt.activities;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.viethp.mt.activities.R;
import com.viethp.mt.common.CommonHelper;
import com.viethp.mt.common.GlobalVariables;
import com.viethp.mt.objects.ChangeEmailResponse;
import com.viethp.mt.objects.ChangePWDResponse;
import com.viethp.mt.objects.ChangePhoneResponse;
import com.viethp.mt.objects.LogoutResponse;
import com.viethp.mt.objects.User;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ATabAccount extends Activity implements OnClickListener{
	
	private Button btn_signout, btn_changeEmail, btn_changePWD, btn_changePhone;
	private TextView tv_username, tv_email, tv_phone;
	EditText edt_email,edt_phone,edt_old_PWD,edt_new_PWD,edt_new_PWD_confirm;
	
	private ProgressDialog progressDialog;
	
	private String change_email, change_phone, change_old_PWD, change_new_PWD, change_new_PWD_confirm;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_tab_account);
		
		initComponents();
		setOnClickListener();
		setInfo();
	}
	
	public void initComponents(){
		btn_signout = (Button) findViewById(R.id.btn_signout);
		btn_changeEmail = (Button) findViewById(R.id.btn_changeEmail);
		btn_changePWD = (Button) findViewById(R.id.btn_changePWD);
		btn_changePhone = (Button) findViewById(R.id.btn_changePhone);
		
		tv_username = (TextView) findViewById(R.id.tv_username);
		tv_email = (TextView) findViewById(R.id.tv_email);
		tv_phone = (TextView) findViewById(R.id.tv_phone);
		
		progressDialog = new ProgressDialog(ATabAccount.this);
		progressDialog.setCancelable(false);
	}
	public void setOnClickListener(){
		btn_signout.setOnClickListener(this);
		btn_changeEmail.setOnClickListener(this);
		btn_changePWD.setOnClickListener(this);
		btn_changePhone.setOnClickListener(this);
	}

	public void setInfo(){
		tv_username.setText(CommonHelper.getUserLogin(ATabAccount.this).getUserName());
		tv_email.setText(CommonHelper.getUserLogin(ATabAccount.this).getEmail());
		tv_phone.setText(CommonHelper.getUserLogin(ATabAccount.this).getPhone());
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_signout:
			showLogoutConfirm().show();
			break;
		case R.id.btn_changeEmail:
			DialogEmail();
			break;
		case R.id.btn_changePWD:
			DialogPassword();
			break;
		case R.id.btn_changePhone:
			DialogPhone();
			break;
		default:
			break;
		}
		
	}
	// logout
	protected Dialog showLogoutConfirm() {
		return new android.app.AlertDialog.Builder(this)
				.setMessage(getString(R.string.logout_confirm))
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								doLogout();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(android.content.DialogInterface dialog,
							int whichButton) {
					}
				}).create();
	}
	
	private void doLogout(){
		if(CommonHelper.isNetworkAvailable(this)){
			startCallServiceLogout();
		} else CommonHelper.showWarning(ATabAccount.this,
				getString(R.string.netstate));
	}
	
	private void startCallServiceLogout() {
		progressDialog.setMessage(getString(R.string.pleasewait));
		progressDialog.show();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Message message = new Message();
				message.obj = callServiceLogout();
				handlerLogout.sendMessage(message);
				
			}
		}).start();
	}
	
	private LogoutResponse callServiceLogout() {

		SoapObject userRequest = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_LOGOUT);
		userRequest.addProperty("user_id", CommonHelper.getUserLogin(ATabAccount.this).getId());

		SoapObject request = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_LOGOUT);
		request.addProperty(GlobalVariables.WS_METHOD_LOGOUT, userRequest);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				GlobalVariables.WS_ENDPOINT,
				GlobalVariables.WS_CONNECTTION_TIME_OUT);
		SoapObject results = null;
		try {
			androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
					+ GlobalVariables.WS_METHOD_LOGOUT, envelope);
		} catch (Exception e) {
			try {
				androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
						+ GlobalVariables.WS_METHOD_LOGOUT, envelope);
			} catch (Exception e1) {
				e1.printStackTrace();
				return null;

			}
		}
		try {
			results = (SoapObject) envelope.getResponse();
		} catch (SoapFault ex3) {
			ex3.printStackTrace();
			return null;
		}
		
		return new LogoutResponse(results);

	}
	
	private Handler handlerLogout = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			progressDialog.dismiss();

			if (msg.obj != null) {
				LogoutResponse response = (LogoutResponse) msg.obj;
				if (response.getErrorCode() != null) {
					CommonHelper.showWarning(ATabAccount.this, response
							.getErrorCode().getErrMsg());
				} else {
					CommonHelper.setLoginMethod(GlobalVariables.LOGOUT, ATabAccount.this);
					Intent alogin = new Intent(getApplicationContext(),ALoginScreen.class);
					startActivity(alogin);
					finish();
				}

			} else {
				CommonHelper.showWarning(ATabAccount.this,
						getString(R.string.disconnect));
			}

		};
	};
	// change email
	public void DialogEmail() {
		
		LayoutInflater layoutInflater 
		  = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View view=layoutInflater.inflate(R.layout.dialog_change_email,null);
		
		edt_email = (EditText) view.findViewById(R.id.edt_change_email);
		edt_email.setText(CommonHelper.getUserLogin(this).getEmail());
			  
		final Dialog dia = new Dialog(this);
		dia.setContentView(view);
		dia.setTitle("Change Email");
		dia.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		dia.setCancelable(false);
		dia.show();
		
		final Button btn_change = (Button) view.findViewById(R.id.btn_change);
		
		if(String.valueOf(edt_email.getText()).equals("")) btn_change.setEnabled(false);
		edt_email.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			@Override
			public void afterTextChanged(Editable s) {
				if(CommonHelper.eMailValidation(String.valueOf(edt_email.getText()))) btn_change.setEnabled(true);
				else btn_change.setEnabled(false);
			}
		});
		
		btn_change.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				change_email = String.valueOf(edt_email.getText());
				   Log.d("email", change_email);
				   if(!change_email.equals(CommonHelper.getUserLogin(ATabAccount.this).getEmail())) {
					   Log.d("email1", CommonHelper.getUserLogin(ATabAccount.this).getEmail());
					   changeEmail(); 
					   dia.dismiss();
				   } else dia.dismiss();
			}
		});
		Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dia.dismiss();
				
			}
		});		
	 }
	
	public void changeEmail(){
		if(CommonHelper.isNetworkAvailable(this)){
			startCallServiceChangeEmail();
		} else CommonHelper.showWarning(ATabAccount.this,
				getString(R.string.netstate));
	}
	
	public void startCallServiceChangeEmail(){
		progressDialog.setMessage(getString(R.string.pleasewait));
		progressDialog.show();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Message message = new Message();
				message.obj = callServiceChangeEmail();
				handlerChangeEmail.sendMessage(message);
				
			}
		}).start();
	}
	
	private ChangeEmailResponse callServiceChangeEmail() {

		SoapObject userRequest = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_CHANGE_EMAIL);
		userRequest.addProperty("user_id", CommonHelper.getUserLogin(ATabAccount.this).getId());
		userRequest.addProperty("new_email", change_email);

		SoapObject request = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_CHANGE_EMAIL);
		request.addProperty(GlobalVariables.WS_METHOD_CHANGE_EMAIL, userRequest);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				GlobalVariables.WS_ENDPOINT,
				GlobalVariables.WS_CONNECTTION_TIME_OUT);
		SoapObject results = null;
		try {
			androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
					+ GlobalVariables.WS_METHOD_CHANGE_EMAIL, envelope);
		} catch (Exception e) {
			try {
				androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
						+ GlobalVariables.WS_METHOD_CHANGE_EMAIL, envelope);
			} catch (Exception e1) {
				e1.printStackTrace();
				return null;

			}
		}
		try {
			results = (SoapObject) envelope.getResponse();
		} catch (SoapFault ex3) {
			ex3.printStackTrace();
			return null;
		}
		
		return new ChangeEmailResponse(results);

	}
	
	private Handler handlerChangeEmail = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			progressDialog.dismiss();

			if (msg.obj != null) {
				ChangeEmailResponse response = (ChangeEmailResponse) msg.obj;
				if (response.getErrorCode() != null) {
					CommonHelper.showWarning(ATabAccount.this, response
							.getErrorCode().getErrMsg());
				} else {
					User user = new User();
					user.setId(CommonHelper.getUserLogin(ATabAccount.this).getId());
					user.setUserName(CommonHelper.getUserLogin(ATabAccount.this).getUserName());
					user.setEmail(change_email);
					user.setPhone(CommonHelper.getUserLogin(ATabAccount.this).getPhone());
					user.setPassWord(CommonHelper.getUserLogin(ATabAccount.this).getPassWord());
					CommonHelper.setUserLogin(user, ATabAccount.this);
					CommonHelper.showToast7s(ATabAccount.this, "Change email successful");
					tv_email.setText(CommonHelper.getUserLogin(ATabAccount.this).getEmail());
				}

			} else {
				CommonHelper.showWarning(ATabAccount.this,
						getString(R.string.disconnect));
			}

		};
	};
	
	// change phone
		public void DialogPhone() {
			
			LayoutInflater layoutInflater 
			  = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final View view=layoutInflater.inflate(R.layout.dialog_change_phone,null);
			
			edt_phone = (EditText) view.findViewById(R.id.edt_change_phone);
			edt_phone.setText(CommonHelper.getUserLogin(this).getPhone());
				  
			final Dialog dia = new Dialog(this);
			dia.setContentView(view);
			dia.setTitle("Change Phone");
			dia.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			dia.setCancelable(false);
			dia.show();
			
			final Button btn_change = (Button) view.findViewById(R.id.btn_change);
			
			if(String.valueOf(edt_phone.getText()).equals("")) btn_change.setEnabled(false);
			edt_phone.addTextChangedListener(new TextWatcher() {
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {}
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
				@Override
				public void afterTextChanged(Editable s) {
					if(String.valueOf(edt_phone.getText()).equals("")) btn_change.setEnabled(false);
					else btn_change.setEnabled(true);
				}
			});
			
			btn_change.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					change_phone = String.valueOf(edt_phone.getText());
					   Log.d("phone", change_phone);
					   if(!change_phone.equals(CommonHelper.getUserLogin(ATabAccount.this).getPhone())) {
						   Log.d("phone1", CommonHelper.getUserLogin(ATabAccount.this).getPhone());
						   changePhone(); 
						   dia.dismiss();
					   } else dia.dismiss();
				}
			});
			Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
			btn_cancel.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					dia.dismiss();
					
				}
			});		
		 }
		
		public void changePhone(){
			if(CommonHelper.isNetworkAvailable(this)){
				startCallServiceChangePhone();
			} else CommonHelper.showWarning(ATabAccount.this,
					getString(R.string.netstate));
		}
		
		public void startCallServiceChangePhone(){
			progressDialog.setMessage(getString(R.string.pleasewait));
			progressDialog.show();
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					Message message = new Message();
					message.obj = callServiceChangePhone();
					handlerChangePhone.sendMessage(message);
					
				}
			}).start();
		}
		
		private ChangePhoneResponse callServiceChangePhone() {

			SoapObject userRequest = new SoapObject(GlobalVariables.WS_NAMESPACE,
					GlobalVariables.WS_METHOD_CHANGE_PHONE);
			userRequest.addProperty("user_id", CommonHelper.getUserLogin(ATabAccount.this).getId());
			userRequest.addProperty("new_phone", change_phone);

			SoapObject request = new SoapObject(GlobalVariables.WS_NAMESPACE,
					GlobalVariables.WS_METHOD_CHANGE_PHONE);
			request.addProperty(GlobalVariables.WS_METHOD_CHANGE_PHONE, userRequest);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);

			HttpTransportSE androidHttpTransport = new HttpTransportSE(
					GlobalVariables.WS_ENDPOINT,
					GlobalVariables.WS_CONNECTTION_TIME_OUT);
			SoapObject results = null;
			try {
				androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
						+ GlobalVariables.WS_METHOD_CHANGE_PHONE, envelope);
			} catch (Exception e) {
				try {
					androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
							+ GlobalVariables.WS_METHOD_CHANGE_PHONE, envelope);
				} catch (Exception e1) {
					e1.printStackTrace();
					return null;

				}
			}
			try {
				results = (SoapObject) envelope.getResponse();
			} catch (SoapFault ex3) {
				ex3.printStackTrace();
				return null;
			}
			
			return new ChangePhoneResponse(results);

		}
		
		private Handler handlerChangePhone = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				progressDialog.dismiss();

				if (msg.obj != null) {
					ChangePhoneResponse response = (ChangePhoneResponse) msg.obj;
					if (response.getErrorCode() != null) {
						CommonHelper.showWarning(ATabAccount.this, response
								.getErrorCode().getErrMsg());
					} else {
						User user = new User();
						user.setId(CommonHelper.getUserLogin(ATabAccount.this).getId());
						user.setUserName(CommonHelper.getUserLogin(ATabAccount.this).getUserName());
						user.setEmail(CommonHelper.getUserLogin(ATabAccount.this).getEmail());
						user.setPhone(change_phone);
						user.setPassWord(CommonHelper.getUserLogin(ATabAccount.this).getPassWord());
						CommonHelper.setUserLogin(user, ATabAccount.this);
						CommonHelper.showToast7s(ATabAccount.this, "Change phone successful");
						tv_phone.setText(CommonHelper.getUserLogin(ATabAccount.this).getPhone());
					}

				} else {
					CommonHelper.showWarning(ATabAccount.this,
							getString(R.string.disconnect));
				}

			};
		};
	
	// change password
	
	public boolean validPassword(String password, String confirm_password, EditText edt_password, EditText edt_confirm_password){
		if(password.equals("")){
			CommonHelper.showToast(getApplicationContext(), "New Password is required.");
			edt_password.requestFocus();
			return false;
		} else if(confirm_password.equals("")){
			CommonHelper.showToast(getApplicationContext(), "Confirm password is required.");
			edt_confirm_password.requestFocus();
			return false;
		} else if(!confirm_password.equals(password)){
			CommonHelper.showToast(getApplicationContext(), "Confirm Password doesn't match.");
			edt_password.requestFocus();
			return false;
		}
		return true;
	}
	
	public void DialogPassword() {
		
		LayoutInflater layoutInflater 
		  = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View view=layoutInflater.inflate(R.layout.dialog_change_pwd,null);
		edt_old_PWD = (EditText) view.findViewById(R.id.edt_old_pwd);
		edt_new_PWD = (EditText) view.findViewById(R.id.edt_new_pwd);
		edt_new_PWD_confirm = (EditText) view.findViewById(R.id.edt_new_pwd_confirm);
			  
		final Dialog dia = new Dialog(this);
		dia.setContentView(view);
		dia.setTitle("Change Password");
		dia.setCancelable(false);
		dia.show();
		
		change_old_PWD = String.valueOf(edt_old_PWD.getText());
		change_new_PWD = String.valueOf(edt_new_PWD.getText());
		change_new_PWD_confirm = String.valueOf(edt_new_PWD_confirm.getText());
		   
		final Button btn_change = (Button) view.findViewById(R.id.btn_change);
		if(change_old_PWD.equals("")&&change_new_PWD_confirm.equals("")&&change_new_PWD_confirm.equals("")){
			btn_change.setEnabled(false);
		} else btn_change.setEnabled(true);
		edt_old_PWD.addTextChangedListener(new TextWatcher() { 
		
	        public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
	                //XXX do something 
	        } 
	        public void onTextChanged(CharSequence s, int start, int before, int count) { 
	                //XXX do something 
	        }
			@Override
			public void afterTextChanged(Editable s) {
				if(!String.valueOf(edt_old_PWD.getText()).equals("")&&!String.valueOf(edt_new_PWD.getText()).equals("")&&!String.valueOf(edt_new_PWD_confirm.getText()).equals("")){
					change_old_PWD = String.valueOf(edt_old_PWD.getText());
					change_new_PWD = String.valueOf(edt_new_PWD.getText());
					change_new_PWD_confirm = String.valueOf(edt_new_PWD_confirm.getText());
					btn_change.setEnabled(true);
				} else btn_change.setEnabled(false);
				
			} 
		}); 
		edt_new_PWD.addTextChangedListener(new TextWatcher() { 
		
	        public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
	                //XXX do something 
	        } 
	        public void onTextChanged(CharSequence s, int start, int before, int count) { 
	                //XXX do something 
	        }
			@Override
			public void afterTextChanged(Editable s) {
				if(!String.valueOf(edt_old_PWD.getText()).equals("")&&!String.valueOf(edt_new_PWD.getText()).equals("")&&!String.valueOf(edt_new_PWD_confirm.getText()).equals("")){
					change_old_PWD = String.valueOf(edt_old_PWD.getText());
					change_new_PWD = String.valueOf(edt_new_PWD.getText());
					change_new_PWD_confirm = String.valueOf(edt_new_PWD_confirm.getText());
					btn_change.setEnabled(true);
				} else btn_change.setEnabled(false);
				
			} 
		}); 
		edt_new_PWD_confirm.addTextChangedListener(new TextWatcher() { 
		
	        public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
	                //XXX do something 
	        } 
	        public void onTextChanged(CharSequence s, int start, int before, int count) { 
	                //XXX do something 
	        }
			@Override
			public void afterTextChanged(Editable s) {
				if(!String.valueOf(edt_old_PWD.getText()).equals("")&&!String.valueOf(edt_new_PWD.getText()).equals("")&&!String.valueOf(edt_new_PWD_confirm.getText()).equals("")){
					change_old_PWD = String.valueOf(edt_old_PWD.getText());
					change_new_PWD = String.valueOf(edt_new_PWD.getText());
					change_new_PWD_confirm = String.valueOf(edt_new_PWD_confirm.getText());
					btn_change.setEnabled(true);
				} else {
					btn_change.setEnabled(false);
					Log.d("confirm", "5"+ ""+String.valueOf(edt_old_PWD.getText())+"bacd"+String.valueOf(edt_new_PWD.getText()));
				}
				
			} 
		}); 
		btn_change.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			   if(change_old_PWD.equals("")){
				   CommonHelper.showToast7s(ATabAccount.this, "Old password is required!");
			   } else {
				   String newPWDmd5 = CommonHelper.toMD5(change_old_PWD);
				   if(!newPWDmd5.equals(CommonHelper.getUserLogin(ATabAccount.this).getPassWord())){
					   CommonHelper.showToast(ATabAccount.this, "Old password incorrect!");
				   } else {
					   if(validPassword(change_new_PWD, change_new_PWD_confirm, edt_new_PWD, edt_new_PWD_confirm)){
						   changePassword();
						   dia.dismiss();
					   }
				   }
			   }
			}
			
		});
		Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dia.dismiss();
				
			}
		});
	 }
	
	public void changePassword(){
		if(CommonHelper.isNetworkAvailable(this)){
			startCallServiceChangePassword();
		} else CommonHelper.showWarning(ATabAccount.this,
				getString(R.string.netstate));
	}
	
	public void startCallServiceChangePassword(){
		progressDialog.setMessage(getString(R.string.pleasewait));
		progressDialog.show();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Message message = new Message();
				message.obj = callServiceChangePassword();
				handlerChangePassword.sendMessage(message);
				
			}
		}).start();
	}
	
	private ChangePWDResponse callServiceChangePassword() {

		SoapObject userRequest = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_CHANGE_PWD);
		userRequest.addProperty("user_id", CommonHelper.getUserLogin(ATabAccount.this).getId());
		userRequest.addProperty("new_password", CommonHelper.toMD5(change_new_PWD));

		SoapObject request = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_CHANGE_PWD);
		request.addProperty(GlobalVariables.WS_METHOD_CHANGE_PWD, userRequest);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				GlobalVariables.WS_ENDPOINT,
				GlobalVariables.WS_CONNECTTION_TIME_OUT);
		SoapObject results = null;
		try {
			androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
					+ GlobalVariables.WS_METHOD_CHANGE_PWD, envelope);
		} catch (Exception e) {
			try {
				androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
						+ GlobalVariables.WS_METHOD_CHANGE_PWD, envelope);
			} catch (Exception e1) {
				e1.printStackTrace();
				return null;

			}
		}
		try {
			results = (SoapObject) envelope.getResponse();
		} catch (SoapFault ex3) {
			ex3.printStackTrace();
			return null;
		}
		
		return new ChangePWDResponse(results);

	}
	
	private Handler handlerChangePassword = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			progressDialog.dismiss();

			if (msg.obj != null) {
				ChangePWDResponse response = (ChangePWDResponse) msg.obj;
				if (response.getErrorCode() != null) {
					CommonHelper.showWarning(ATabAccount.this, response
							.getErrorCode().getErrMsg());
				} else {
					User user = new User();
					user.setId(CommonHelper.getUserLogin(ATabAccount.this).getId());
					user.setUserName(CommonHelper.getUserLogin(ATabAccount.this).getUserName());
					user.setEmail(CommonHelper.getUserLogin(ATabAccount.this).getEmail());
					user.setPhone(CommonHelper.getUserLogin(ATabAccount.this).getPhone());
					user.setPassWord(CommonHelper.toMD5(change_new_PWD));
					CommonHelper.setUserLogin(user, ATabAccount.this);
					CommonHelper.showToast7s(ATabAccount.this, "Change password successful");
				}

			} else {
				CommonHelper.showWarning(ATabAccount.this,
						getString(R.string.disconnect));
			}

		};
	};
}
